using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The UserContactTableDB class is responsible for interacting with the database to retrieve and store information
/// about UserContactTable objects.
/// </summary>
public class UserContactTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of UserContactTable from the underlying datasource.
/// </summary>
/// <param name="userContactKey">The unique UserContactKey of the UserContactTable in the database.</param>
/// <returns>An UserContactTable when the UserContactKey was found in the database, or null otherwise.</returns>
public static UserContactTable GetItem(int userContactKey)
{
UserContactTable myUserContactTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserContactTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userContactKey", userContactKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myUserContactTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myUserContactTable;
}
}

/// <summary>
/// Returns a list with UserContactTable objects.
/// </summary>
/// <returns>A generics List with the UserContactTable objects.</returns>
public static UserContactTableList GetList()
{
UserContactTableList tempList = new UserContactTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserContactTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with UserContactTable objects.
/// </summary>
/// <returns>A generics List with the UserContactTable objects.</returns>
public static UserContactTableList GetListByUserAccountKey(int UserAccountKey)
{
UserContactTableList tempList = new UserContactTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserContactTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a UserContactTable in the database.
/// </summary>
/// <param name="myUserContactTable">The UserContactTable instance to save.</param>
/// <returns>The new UserContactKey if the UserContactTable is new in the database or the existing UserContactKey when an item was updated.</returns>
public static int Save(UserContactTable myUserContactTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserContactTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myUserContactTable.UserContactKey == -1)
{myCommand.Parameters.AddWithValue("@userContactKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userContactKey", myUserContactTable.UserContactKey);
}
if (myUserContactTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myUserContactTable.UserAccountKey);
}
if (string.IsNullOrEmpty(myUserContactTable.MobileNumber))
{
myCommand.Parameters.AddWithValue("@mobileNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@mobileNumber", myUserContactTable.MobileNumber);
}
if (string.IsNullOrEmpty(myUserContactTable.HomePhoneNumber))
{
myCommand.Parameters.AddWithValue("@homePhoneNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@homePhoneNumber", myUserContactTable.HomePhoneNumber);
}
if (string.IsNullOrEmpty(myUserContactTable.OtherNumber))
{
myCommand.Parameters.AddWithValue("@otherNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@otherNumber", myUserContactTable.OtherNumber);
}
if (string.IsNullOrEmpty(myUserContactTable.EmailAddress))
{
myCommand.Parameters.AddWithValue("@emailAddress", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@emailAddress", myUserContactTable.EmailAddress);
}
if (myUserContactTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myUserContactTable.Status);
}
if (myUserContactTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myUserContactTable.CreatedDateTime);
}
if (myUserContactTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myUserContactTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a UserContactTable from the database.
/// </summary>
/// <param name="userContactKey">The UserContactKey of the UserContactTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int userContactKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserContactTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@userContactKey", userContactKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the UserContactTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static UserContactTable FillDataRecord(IDataRecord myDataRecord )
{
UserContactTable myUserContactTable = new UserContactTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserContactKey")))
{
myUserContactTable.UserContactKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserContactKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myUserContactTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MobileNumber")))
{
myUserContactTable.MobileNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("MobileNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("HomePhoneNumber")))
{
myUserContactTable.HomePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("HomePhoneNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherNumber")))
{
myUserContactTable.OtherNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmailAddress")))
{
myUserContactTable.EmailAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("EmailAddress"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myUserContactTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myUserContactTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myUserContactTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myUserContactTable;
}
}

 } 
